package com.example.demo.util;

import cn.hutool.json.JSONObject;
import com.example.demo.GlobalException.BaseErrorInfoInterface;
import com.example.demo.GlobalException.CommonEnum;


/**
 * 统一返回格式
 * @author chenhao
 */
public class ResultBody {

    /**
     * 响应代码
     * */
    private Integer code;

    /**
     * 相应消息
     * */
    private String msg;

    /**
     * 响应数量
     * */
    private Integer count;

    /**
     * 响应数据
     * */
    private Object data;

    public ResultBody() {
    }

    public ResultBody(BaseErrorInfoInterface errorInfo) {
        this.code = errorInfo.getResultCode();
        this.msg = errorInfo.getResultMsg();
        this.count = 0;
        this.data = null;
    }

    public ResultBody(Integer code, String msg, Integer count, Object data) {
        this.code = code;
        this.msg = msg;
        this.count = count;
        this.data = data;
    }


    /**
     * 成功-数据表格
     * @param data 响应数据
     * @param count 响应分页总数
     * @return 返回格式
     */
    public static ResultBody success(Integer count, Object data) {
        ResultBody body = new ResultBody();
        body.setCode(0);
        body.setMsg("");
        body.setCount(count);
        body.setData(data);
        return body;
    }

    /**
     * 成功——带数据
     * @param data 响应数据
     * @return 返回格式
     */
    public static ResultBody success(Object data) {
        ResultBody body = new ResultBody();
        body.setCode(CommonEnum.SUCCESS.getResultCode());
        body.setMsg(CommonEnum.SUCCESS.getResultMsg());
        body.setCount(0);
        body.setData(data);
        return body;
    }

    /** 成功——带单个自定义数据，key,value对
     * @param key key
     * @param data value
     * @return 返回格式
     */
    public static ResultBody success(String key,Object data) {
        ResultBody body = new ResultBody();
        JSONObject object = new JSONObject();
        object.put(key,data);
        body.setCode(CommonEnum.SUCCESS.getResultCode());
        body.setMsg(CommonEnum.SUCCESS.getResultMsg());
        body.setCount(0);
        body.setData(object);
        return body;
    }

    /**
     * 成功——不带消息
     * @return 返回格式
     */
    public static ResultBody success() {
        ResultBody body = new ResultBody();
        body.setCode(CommonEnum.SUCCESS.getResultCode());
        body.setMsg(CommonEnum.SUCCESS.getResultMsg());
        body.setCount(0);
        body.setData(null);
        return body;
    }

    /**
     * 失败
     * @param errorInfo 错误信息，返回状态码，具体看CommonEnum.java
     * */
    public static ResultBody error(BaseErrorInfoInterface errorInfo){
        ResultBody body = new ResultBody();
        body.setCode(errorInfo.getResultCode());
        body.setMsg(errorInfo.getResultMsg());
        body.setCount(0);
        body.setData(null);
        return body;
    }

    /**
     * 失败-自定义消息
     */
    public static ResultBody error(String message) {
        ResultBody body = new ResultBody();
        body.setCode(400);
        body.setMsg(message);
        body.setData(null);
        body.setCount(0);
        return body;
    }

    /**
     * 失败——自定义状态码、消息
     *
     */
    public static ResultBody error(Integer code, String message) {
        ResultBody body = new ResultBody();
        body.setCode(code);
        body.setMsg(message);
        body.setData(null);
        body.setCount(0);
        return body;
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public Integer getCount() {
        return count;
    }

    public void setCount(Integer count) {
        this.count = count;
    }

    public Object getData() {
        return data;
    }

    public void setData(Object data) {
        this.data = data;
    }

    @Override
    public String toString() {
        return "ResultBody{" +
                "code='" + code + '\'' +
                ", msg='" + msg + '\'' +
                ", count=" + count +
                ", data=" + data +
                '}';
    }
}
